extends Node

signal inventory_updated

var items = []
var selected_slot_index = -1

func add_item(item_id: String, quantity: int = 1):
	# 查找是否存在相同可堆叠物品
	for i in range(items.size()):
		if items[i].id == item_id and items[i].stackable:
			items[i].quantity += quantity
			inventory_updated.emit()
			return
	
	# 添加新物品
	var new_item = {
		"id": item_id,
		"name": "Item " + item_id,
		"texture": load("res://icon.svg"),
		"stackable": true,
		"quantity": quantity
	}
	items.append(new_item)
	inventory_updated.emit()

func remove_item(index: int, quantity: int = 1):
	if index >= 0 && index < items.size():
		items[index].quantity -= quantity
		if items[index].quantity <= 0:
			items.remove_at(index)
			selected_slot_index = -1
		inventory_updated.emit()

func select_slot(index: int):
	selected_slot_index = index
	inventory_updated.emit()
